package vip.xiaonuo.core.util;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.Result;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.SneakyThrows;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * minio存储
 *
 * @author yanwei
 * @create 2022-08-31
 */
public class MinioObjUtil {

    private static final String OPEN_LINK_URL = "http://xxxxxx:15610";
    private static final String accessKey = "1vXpuxxF7jAy";
    private static final String secretKey = "CIbWf1xx4ptZhCu2TxhykmK1fObVcPwMIV";

    private static final String bucketName = "plan";
   private static   MinioClient minioClient;

    static {
        try {
            minioClient = new MinioClient(OPEN_LINK_URL, accessKey, secretKey);
        } catch (InvalidEndpointException e) {
            throw new RuntimeException(e);
        } catch (InvalidPortException e) {
            throw new RuntimeException(e);
        }
    }

    @SneakyThrows
    public static String upLoad(String fileName, InputStream inputStream) {
        PutObjectOptions putObjectOptions = new PutObjectOptions(inputStream.available(), PutObjectOptions.MAX_PART_SIZE);
        putObjectOptions.setContentType(getContentType(fileName));
        minioClient.putObject(bucketName, fileName,inputStream, putObjectOptions);
        return StrUtil.join("/",OPEN_LINK_URL,bucketName,fileName);
    }

    @SneakyThrows
    public static List<String> sync(){
        ArrayList<String> linkLists = Lists.newArrayList();
        List<Bucket> buckets = minioClient.listBuckets();
        for (Bucket bucket : buckets) {
            Iterable<Result<Item>> resultIterable = minioClient.listObjects(bucket.name());
            Iterator<Result<Item>> iterator = resultIterable.iterator();
            while (iterator.hasNext()) {
                Result<Item> itemResult = iterator.next();
                linkLists.add(StrUtil.join("/",OPEN_LINK_URL,bucket.name(),itemResult.get().objectName()));
            }
        }
        return linkLists;
    }


    /**
     * 通过文件名判断并获取OSS服务文件上传时文件的contentType
     *
     * @param fileName 文件名
     * @return 文件的contentType
     */
    public static String getContentType(String fileName) {
        //文件的后缀名
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        /**
         * "video/mp4",
         *           "video/ogg",
         *           "video/flv",
         *           "video/avi",
         *           "video/wmv",
         *           "video/rmvb",
         *           "video/mov",
         */
        if (".mp4".equalsIgnoreCase(fileExtension) || ".ogg".equalsIgnoreCase(fileExtension) || ".flv".equalsIgnoreCase(fileExtension) || ".avi".equals(fileExtension) || ".wmv".equals(fileExtension) || ".rmvb".equalsIgnoreCase(fileExtension) || ".mov".equalsIgnoreCase(fileExtension)){
            return "video/mp4";
        }

        if (".bmp".equalsIgnoreCase(fileExtension)) {
            return "image/bmp";
        }
        if (".gif".equalsIgnoreCase(fileExtension)) {
            return "image/gif";
        }
        if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension) || ".png".equalsIgnoreCase(fileExtension)) {
            return "image/jpeg";
        }
        if (".html".equalsIgnoreCase(fileExtension)) {
            return "text/html";
        }
        if (".txt".equalsIgnoreCase(fileExtension)) {
            return "text/plain";
        }
        if (".vsd".equalsIgnoreCase(fileExtension)) {
            return "application/vnd.visio";
        }
        if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
            return "application/vnd.ms-powerpoint";
        }
        if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
            return "application/msword";
        }
        if (".xml".equalsIgnoreCase(fileExtension)) {
            return "text/xml";
        }
        //默认返回类型
        return "image/jpeg";
    }

}
